package models

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"os"

	"sync"
	"time"
)

func init() {

	var once sync.Once
	once.Do(func() {
		init_db("mysql", `default`)

		userInfo := UserInfo{}
		userInfo.ID = 1
		userInfo.PassWord = `12345678`
		userInfo.Phone = `130000000`
		userInfo.RegisterTime = time.Now()

		userInfo.Insert()

		info := SecInfo{}
		info.UserInfo = &userInfo
		info.Gas = 10
		info.IllegalEntry = false
		info.Tem = 23
		info.PickTime = time.Now()
		info.Insert()

	})

}

func init_db(driverName, aliasName string) {
	var err error
	orm.Debug = true
	err = orm.RegisterDriver(driverName, orm.DRMySQL)

	if err != nil {
		fmt.Println("register db  driver error ", err)
		os.Exit(2)
	}

	dataSource := beego.AppConfig.String("source")
	fmt.Println("data Source ", dataSource)

	err = orm.RegisterDataBase(aliasName, driverName, dataSource, 30)

	if err != nil {
		fmt.Println("error register data base error ", err.Error())
		os.Exit(2)
	}

	orm.RegisterModel(&UserInfo{})
	orm.RegisterModel(&SecInfo{})
	orm.RegisterModel(&UserLog{})
	orm.RegisterModel(&Admin{})
	orm.RegisterModel(&SubMac{})

}
